package edu.ucdavis.gwt.gis.client.state;

import com.google.gwt.dom.client.IFrameElement;
import com.google.gwt.user.client.Timer;

import edu.ucdavis.gwt.gis.client.DataManager;
import edu.ucdavis.gwt.gis.client.Debugger;

public class CloudStore {
	
	public static CloudStore INSTANCE = new CloudStore(); 
	
	public CloudSaveHandler handler = null;
	
	private IFrameElement frame;
	private String data = "";
	private String id = "";
	private String name = "";
	
	private boolean saving = false;
	private boolean dataSet = false;
	private boolean nameSet = false;
	private boolean idSet = false;
	private boolean passwordSet = false;
	private String url = "";
	private String origin = "";
	
	protected CloudStore() {
		url = DataManager.INSTANCE.getConfig().getCloudStoreUrl();
		if( url.contains("://") ) {
			String[] parts = url.split("//");
			origin = parts[0]+"//"+parts[1].replaceAll("/.*", "");
			
			Debugger.INSTANCE.catchException(url+" - "+origin, "", "CloudStore", "");
			init(origin);
		}
	}
	
	private native void init(String url) /*-{
		var ref = this;
		function receiveMessage(event) {
           if (event.origin !== url)
                   return;
			ref.@edu.ucdavis.gwt.gis.client.state.CloudStore::onAck(Ljava/lang/String;)(event.data);
       }
       if( !window.addEventListener ) {
       	 $wnd.attachEvent("onmessage", receiveMessage);
       } else {
         $wnd.addEventListener("message", receiveMessage, false);
       }
	}-*/;
	
	public void save(CloudSaveHandler handler, String id, String name, String data) {
		if( saving ) return;
		
		saving = true;
		clearResponses();
		this.handler = handler;
		this.name = name;
		this.id = id;
		this.data = data;
		
		frame = createIFrame(url);
	}

	@SuppressWarnings("unused")
	private void onFrameLoad() {
		sendMessage(frame, "id", id, origin);
		sendMessage(frame, "name", name, origin);
		sendMessage(frame, "data", data, origin);
		sendMessage(frame, "password", "2and2equals5", origin);
	}
	
	private native IFrameElement createIFrame(String url) /*-{
		var ref = this;
		var frame = $wnd.document.createElement("iframe");
		frame.style.display = "none";
		frame.src = url;
		frame.onload = function() {
			ref.@edu.ucdavis.gwt.gis.client.state.CloudStore::onFrameLoad()();
		}
		$wnd.document.body.appendChild(frame);
		return frame;
	}-*/;
	
	private native void removeFrame(IFrameElement frame) /*-{
		$wnd.document.body.removeChild(frame);
	}-*/;
	
	private native void sendMessage(IFrameElement frame, String key, String value, String origin) /*-{
		frame.contentWindow.postMessage("__"+key+"_"+value, origin);
	}-*/;
	
	@SuppressWarnings("unused")
	private void onAck(String ack) {
		String id = ack.replaceAll("__ack_", "");
		
		if( id.contentEquals("data") ) dataSet = true;
		else if( id.contentEquals("name") ) nameSet = true;
		else if( id.contentEquals("id") ) idSet = true;
		else if( id.contentEquals("password") ) passwordSet = true;
		else if( id.contentEquals("done") ) {
			Timer t = new Timer() {
				@Override
				public void run() {
					removeFrame(frame);
					frame = null;
				}
			};
			saving = false;
			handler.onSaveComplete();
			return;
		}
		
		if( dataSet && nameSet && idSet && passwordSet ) {
			sendMessage(frame, "done", "", origin);
		}
	}
	
	private void clearResponses() {
		dataSet = false;
		nameSet = false;
		idSet = false;
		passwordSet = false;
	}

	
}
